package com.example.wishstar.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.wishstar.MainActivity;
import com.example.wishstar.MyApplication;
import com.example.wishstar.R;
import com.example.wishstar.databinding.FragmentStarBinding;
import com.example.wishstar.model.Star;
import com.example.wishstar.view.StarDialogFragment;
import com.example.wishstar.viewmodel.StarViewModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StarFragment extends Fragment implements StarDialogFragment.OnCallBack, StarViewModel.StarCallBack{
    private String TAG = "StarFragment";
    private FragmentStarBinding fragmentStarBinding;
    private StarViewModel starViewModel;
    private SwipeRefreshLayout starSwipeRefresh;

    private List<Point> postions = new ArrayList<>();
    private List<View> views = new ArrayList<>();
    private int x0, x;
    private int STAR_DISTANCE = 80;
    private int postion;
    private int starNum;
    private int postionNow;
    private StarDialogFragment starDialogFragment = null;
    private Star star = null;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        starViewModel = new ViewModelProvider(this).get(StarViewModel.class);
        fragmentStarBinding = FragmentStarBinding.inflate(inflater, container, false);
        View root = fragmentStarBinding.getRoot();

        TextView title = root.findViewById(R.id.common_title);
        title.setText(getContext().getString(R.string.title_star));

        initView();
        initPostions();
        postion = 0;
        starNum = 0;
        postionNow = 0;

        for(int i = 0; i < 5; i++) {
            int finalI = i;
            views.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    starViewModel.setCallBack(StarFragment.this);
                    starViewModel.getStar(finalI);
                }
            });
        }

        //starViewModel;
        starSwipeRefresh = fragmentStarBinding.starSwipeRefresh;
        starSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                starViewModel.refreshStar();
                starSwipeRefresh.setRefreshing(false);
            }
        });

        return root;
    }

    private void getNextStar() {
        views.get(postionNow).setX(postions.get(postion).x);
        views.get(postionNow).setY(postions.get(postion).y);
        postion++;
        starNum++;
        if (starNum == 5) {
            starNum = 0;
        }
        if (postion == 15) {
            postion = 0;
        }
    }

    private void initView() {
        View image1 = (View) fragmentStarBinding.lottieAnimationView0;

        views.add(image1);

        View image2 = (View) fragmentStarBinding.lottieAnimationView1;
        views.add(image2);

        View image3 = (View) fragmentStarBinding.lottieAnimationView2;
        views.add(image3);

        View image4 = (View) fragmentStarBinding.lottieAnimationView3;
        views.add(image4);

        View image5 = (View) fragmentStarBinding.lottieAnimationView4;
        views.add(image5);

        x0 = image1.getLayoutParams().width;
        Log.e(TAG, "x0: " + x0);
    }

    private void initPostions() {
        x = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        Log.e(TAG, "x: " + x);
        for(int i = 0; i < 5; i++) {
            Point point1 = new Point();
            point1.x = (float) STAR_DISTANCE;
            point1.y = (float) STAR_DISTANCE + (float) ((STAR_DISTANCE + x0) * i);

            Point point2 = new Point();
            point2.x = (float) (x / 2f - x0 / 2f);
            point2.y = (float) STAR_DISTANCE + (float) ((STAR_DISTANCE + x0) * i);

            Point point3 = new Point();
            point3.x = (float) (x - STAR_DISTANCE - x0);
            point3.y = (float) STAR_DISTANCE + (float) ((STAR_DISTANCE + x0) * i);

            postions.add(point1);
            postions.add(point2);
            postions.add(point3);
        }
        Collections.shuffle(postions);
    }

    public void onDestroyView() {
        super.onDestroyView();
        fragmentStarBinding = null;
    }

    @Override
    public void callback(int wishId, int isLike) {
        if (isLike == 1) {
            starViewModel.addWishFavourNum(wishId);
        }
        getNextStar();
    }

    @Override
    public void callback2(int postion, Star star) {
        Log.e(TAG, "callback2 : " + star.toString());
        this.star = star;
        postionNow = postion;
        starDialogFragment = new StarDialogFragment();
        starDialogFragment.setCallBack(StarFragment.this);

        Bundle bundle = new Bundle();
        bundle.putInt("wishId", star.wishId);
        bundle.putString("wishTitle", star.wishTitle);
        bundle.putString("wishDetails", star.wishDetails);
        bundle.putString("finishTime", star.finishTime);
        starDialogFragment.setArguments(bundle);
        starDialogFragment.show(getFragmentManager(), "wanglong");
    }

    private class Point {
        public float x;
        public float y;
    }
}
